import axios from "axios";
import store from "../store";
import { Message } from "element-ui";
import router from "@/router";
axios.defaults.headers["Content-Type"] = "application/json";
const service = axios.create({
  baseURL: '/dld',
  timeout: 30000,
});
service.interceptors.request.use(
  (config) => {
    // var token = store.state.token
    config.headers["satoken"] = "Bearer " + store.state.token;
    return config;
  },
  (error) => {
    Message.error(error)
    Promise.reject(error);
  }
);
service.interceptors.response.use(
  (response) => {
    const data = response.data
    if (data.code === 200) {
      return data;
    }
    if (data.code === 500) {
      Message.error(data.message)
      return Promise.reject(new Error(data.message))
    }
    if (data.code === 401) {
      router.push("/")
      return;
    }
    return Promise.reject(data.message);
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default service;
